package com.github.flying.cattle.mdg.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {

    public static Date currentDate(Date date) {
        if (date == null) date = new Date();
        return parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date currentDate() {
        return currentDate(new Date());
    }

    public static String formatDate(Date date, String format) {
        if (date == null) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return simpleDateFormat.format(date);
    }

    public static Date parseDate(Date date, String format) {
        if (date == null) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return parseDate(simpleDateFormat.format(date), format);
    }

    public static Date parseDate(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            return simpleDateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addOneMonth(Date date) {
        return addMonth(date, 1);
    }

    public static Date addMonth(Date date, int totalMonthCountToAdd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, totalMonthCountToAdd);
        return calendar.getTime();
    }

    public static Date parseBirthday(String date) {
        return parseDate(date, "yyyy-MM-dd");
    }

    public static String formatBirthday(String date) {
        return formatDate(parseBirthday(date), "yyyy-MM-dd");
    }

    public static String formatBirthday(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }
}
